package com.aaxis.microservice.training.demo1.message;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * Created by terrence on 2018/10/22.
 */
@Component
@Slf4j
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, Object> mKafkaTemplate;



    /**
     * send message
     *
     * @param topicName
     * @param data
     */
    public void sendMessage(String topicName, Object data) {
        log.info("sendMessage：{}", data);
        try {
            mKafkaTemplate.send(topicName, data);
            log.info("send success!");
        } catch (Exception e) {
            log.error("sendMessage() error occurs, topic:{}, data:{}, error details:{}", topicName, data,
                    ExceptionUtils.getStackTrace(e));
        }
    }
}

